package org.example.operation;
import java.util.*;
import org.example.model.*;
import org.example.trans.*;
import org.example.inout.*;

/**
 * @author lenovo
 */

public class StudentOperation {
    //将name,gender,birthday添加到HashMap中,随机生成num，并写入文件，返回num
    public static String addStudent(String name,String gender, String birthday){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        Student student = new Student();
        Score score = new Score();
        student.setNum(DataOperation.randomNum());
        if(student.getNum() == null){
            return null;
        }
        student.setName(name);
        student.setGender(gender);
        student.setBirthday(birthday);
        score.setStudent(student);
        hashMap.put(student.getNum(),score);
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
        return student.getNum();
    }
    //删除HashMap中num对应值的Student，并写入文件
    public static void deleteStudent(String num){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        hashMap.remove(num);
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //修改HashMap中num对应值的name,gender,birthday，并写入文件
    public static void modifyStudent(String num,String name,String gender,
                                     String birthday){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        Student student = new Student();
        student.setNum(num);
        student.setName(name);
        student.setGender(gender);
        student.setBirthday(birthday);
        if(name.isEmpty()){
            student.setName(hashMap.get(num).getStudent().getName());
        }
        if(gender.isEmpty()){
            student.setGender(hashMap.get(num).getStudent().getGender());
        }
        if(birthday.isEmpty()){
            student.setBirthday(hashMap.get(num).getStudent().getBirthday());
        }
        hashMap.get(num).setStudent(student);
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
    //检测HashMap中是否不存在num对应Student
    public static boolean isNotStudent(String num){
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        return !hashMap.containsKey(num);
    }
    //清空hashMap
    public static void clearStudent(){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        hashMap.clear();
        Save.setHashMapScore(hashMap);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
    }
}
